import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;


public class HbaseApiStu {
    //插入（添加）数据  put '表名'，‘行键’，'列族：列'，'值'
    public static void put_data(
            Configuration conf,
            String tablename,
            String rowkey,
            String cf,
            String c,
            String value) throws Exception {
        //生成一个对象
        HTable hTable = new HTable(conf, tablename);
        //生成put对象  按住ctrl点击类名
        Put put = new Put(Bytes.toBytes(rowkey));
        //添加数据
        put.addColumn(
                Bytes.toBytes(cf),//列族
                Bytes.toBytes(c),//列
                Bytes.toBytes(value) //值
        );

        //使用put上传
        hTable.put(put);

    }

    //扫描全表 scan 'student'
    public static void get_scanner(
            Configuration conf,
            String table_name //表名
    ) throws Exception {
        //htable对象的生成
        HTable hTable = new HTable(conf, table_name);
        //生成Scan对象
        Scan scan = new Scan();
        //得到数据
        ResultScanner scanner = hTable.getScanner(scan);
        //循环得到每一行数据
        for (Result result : scanner) {
            //得到整个一行表格的数据
            Cell[] cells = result.rawCells();
            //得到具体的数据
            for (Cell cell : cells) {
                //打印行键
                System.out.print(new String(CellUtil.cloneRow(cell)) + "\t\t");
                //打印列族
                System.out.print(new String(CellUtil.cloneFamily(cell)) + ":");
                //打印列
                System.out.print(new String(CellUtil.cloneQualifier(cell)) + "\t\t");
                //打印值
                System.out.print("value=" + new String(CellUtil.cloneValue(cell)) + ",");
                System.out.println("timestamp=" + cell.getTimestamp());
            }
        }

    }

    //get获取某一行某一列数据
    public static void get_row(Configuration conf, String tableName, String rowkey, String family, String column) throws IOException {
        //得到table对象用于数据的查询
        HTable table = new HTable(conf, tableName);
        //get对象
        Get get = new Get(rowkey.getBytes());
        //怎么样将数据放到get对象里
        get.addColumn(family.getBytes(), column.getBytes());
        //查询数据
        Result result = table.get(get);
        //得到表格的数据
        Cell[] cells = result.rawCells();
        for (Cell cell : cells) {
            System.out.println("值：" +new String(CellUtil.cloneValue(cell)) );
        }

    }

    //主方法
    public static void main(String[] args)
            throws Exception {
        //配置对象
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "192.168.126.139");
        conf.set("hbase.zookeeper.property.clientPort", "2181");
        //插入数据
/*        put_data(conf, "student",
                "rowkey111",
                "person",
                "name",
                "zhangsan");*/
/*        get_scanner(conf, "student");*/
        get_row(conf,"student","rowkey111","person","name");

    }
}
